package com.gimi.cloud.bbp.commonService.order.impl;

import java.util.Date;
import java.util.Random;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.bbp.common.constant.DateFormatConstants;
import com.gimi.cloud.bbp.commonService.order.OrderCodeService;
import com.gimi.cloud.bbp.dao.dao.order.OrderCodeDao;
import com.gimi.cloud.bbp.entity.order.OrderCodeEntity;

/**
 * 订单编码服务
 * @author 李林峰
 */
@Service
@Transactional
public class OrderCodeServiceImpl implements OrderCodeService {
	@Autowired OrderCodeDao orderCodeDao;

	@Override
	public String CreateOrderID() {
		String selfGrowthID = String.format("%07d", getSelfGrowthID());
		selfGrowthID = selfGrowthID.substring(selfGrowthID.length()-7);
		return new StringBuffer().append(DateFormatUtils.format(new Date(), DateFormatConstants.DIGIT14NUMBER_DATETIME_FORMAT))
				.append(String.format("%04d", new Random().nextInt(10000))).append(selfGrowthID).toString();
	}

	@Override
	public Integer getSelfGrowthID() {
		OrderCodeEntity orderCodeEntity = new OrderCodeEntity();
		orderCodeDao.baseInsert(orderCodeEntity);
		return orderCodeEntity.getId();
	}

}
